// Set utils function parseTime to filter
export { parseTime } from '@/utils'
import config from './config'
import { floatMul } from '@/utils/calculate'
export * from './galaxyFilters'
export * from './commonFilter'
// Filter for user status
export const userStatusBgFilter = (status: string | number) => {
  status = status + ''
  const statusMap: { [key: string]: string } = {
    1: 'success', // 激活
    0: 'info', // 待激活
    2: 'danger' //停用
  }
  return statusMap[status]
}
export const userStatusFilter = (status: string | number) => {
  status = status + ''
  const statusMap: { [key: string]: string } = {
    1: '已激活', // 激活
    0: '待激活', // 待激活
    2: '停用' //停用
  }
  return statusMap[status]
}

// Filter to uppercase the first character
export const uppercaseFirstChar = (str: string) => {
  return str.charAt(0).toUpperCase() + str.slice(1)
}
/*
 * number：要格式化的数字
 * decimals：保留几位小数
 * dec_point：小数点符号
 * thousands_sep：千分位符号
 * */
export const amountFormat = (
  number: any = 0,
  decimals = 2,
  dec_point = '.',
  thousands_sep = ',',
  isAmount = true
) => {
  number = (number + '').replace(/[^0-9+-Ee.]/g, '')
  var n = !isFinite(+number) ? 0 : +number,
    prec = !isFinite(+decimals) ? 0 : Math.abs(decimals),
    sep = typeof thousands_sep === 'undefined' ? ',' : thousands_sep,
    dec = typeof dec_point === 'undefined' ? '.' : dec_point,
    s: any = '',
    toFixedFix = function(n: any, prec: any) {
      var k = Math.pow(10, prec)
      // 精度问题，小数四则运算会有精度问题
      return isAmount
        ? '' + Math.ceil(floatMul(n, k)) / k / 100
        : '' + Math.ceil(floatMul(n, k)) / k
    }

  s = (prec >= 0 ? toFixedFix(n, prec) : '' + Math.round(n)).split('.')
  var re = /(-?\d+)(\d{3})/
  while (re.test(s[0])) {
    s[0] = s[0].replace(re, '$1' + sep + '$2')
  }

  if ((s[1] || '').length < prec) {
    s[1] = s[1] || ''
    s[1] += new Array(prec - s[1].length + 1).join('0')
  }
  return s.join(dec)
}
export const amountFormatDashboard = (
  number: any = 0,
  decimals = 2,
  dec_point = '.',
  thousands_sep = ',',
  isAmount = true
) => {
  number = (number + '').replace(/[^0-9+-Ee.]/g, '')
  var n = !isFinite(+number) ? 0 : +number,
    prec = !isFinite(+decimals) ? 0 : Math.abs(decimals),
    sep = typeof thousands_sep === 'undefined' ? ',' : thousands_sep,
    dec = typeof dec_point === 'undefined' ? '.' : dec_point,
    s: any = '',
    toFixedFix = function(n: any, prec: any) {
      var k = Math.pow(10, prec)
      // 精度问题，小数四则运算会有精度问题
      return isAmount
        ? '' + Math.ceil(n * k) / k / 10000
        : '' + Math.ceil(n * k) / k
    }

  s = (prec ? toFixedFix(n, prec) : '' + Math.round(n)).split('.')
  var re = /(-?\d+)(\d{3})/
  while (re.test(s[0])) {
    s[0] = s[0].replace(re, '$1' + sep + '$2')
  }

  if ((s[1] || '').length < prec) {
    s[1] = s[1] || ''
    s[1] += new Array(prec - s[1].length + 1).join('0')
  }
  return s.join(dec)
}
// --------------------- 通用枚举处理：start ----------------------------
export const ableOrNotFormat = (str: string) => {
  if ((config.common.ableOrNotOptions as any)[str]) {
    return (config.common.ableOrNotOptions as any)[str]
  } else {
    return str
  }
}
export const validOrNotFormat = (str: string) => {
  if ((config.common.validOrNotOptions as any)[str]) {
    return (config.common.validOrNotOptions as any)[str]
  } else {
    return str
  }
}
export const yesOrNoFormat = (str: string) => {
  if ((config.common.YesOrNoOptions as any)[str]) {
    return (config.common.YesOrNoOptions as any)[str]
  } else {
    return str
  }
}
// --------------------- 通用枚举处理：end ----------------------------

// --------------------- 资产平台枚举处理：start ----------------------------
export const assetStatusFormat = (str: string) => {
  if ((config.channel.assetStatusOptions as any)[str]) {
    return (config.channel.assetStatusOptions as any)[str]
  } else {
    return str
  }
}
// --------------------- 资产平台枚举处理：end ----------------------------

// --------------------- 核心系统枚举处理：start ----------------------------
export const checkStatusFormat = (str: string) => {
  if ((config.core.checkStatusOptions as any)[str]) {
    return (config.core.checkStatusOptions as any)[str]
  } else {
    return str
  }
}

export const intCalcTypeArrayFormat = (str: string = '') => {
  let tempStr: any = []
  let arrys = str.split(',')
  arrys.forEach(item => {
    if ((config.core.intCalcTypeOptions as any)[item]) {
      tempStr.push((config.core.intCalcTypeOptions as any)[item])
    }
  })
  if (tempStr.length) {
    return tempStr.join(',')
  } else {
    return str
  }
}

export const intCalcTypeFormat = (str: string = '') => {
  if ((config.core.intCalcTypeOptions as any)[str]) {
    return (config.core.intCalcTypeOptions as any)[str]
  } else {
    return str
  }
}
export const attachmentTypeFormat = (str: string = '') => {
  if ((config.core.attachmentTypeOptions as any)[str]) {
    return (config.core.attachmentTypeOptions as any)[str]
  } else {
    return str
  }
}

export const coreAssetStatusFormat = (str: string = '') => {
  if ((config.core.coreAssetStatusOptions as any)[str]) {
    return (config.core.coreAssetStatusOptions as any)[str]
  } else {
    return str
  }
}

export const accrualTypeFormat = (str: string = '') => {
  if ((config.core.accrualTypeOptions as any)[str]) {
    return (config.core.accrualTypeOptions as any)[str]
  } else {
    return str
  }
}
export const graceTypeFormat = (str: string = '') => {
  if ((config.core.graceTypeOptions as any)[str]) {
    return (config.core.graceTypeOptions as any)[str]
  } else {
    return str
  }
}
export const creditCycleTypeFormat = (str: string = '') => {
  if ((config.core.creditCycleTypeOptions as any)[str]) {
    return (config.core.creditCycleTypeOptions as any)[str]
  } else {
    return str
  }
}
export const repayWayArrayFormat = (str: string = '') => {
  let tempStr: any = []
  let arrys = str.split(',')
  arrys.forEach(item => {
    if ((config.core.repayWayOptions as any)[item]) {
      tempStr.push((config.core.repayWayOptions as any)[item])
    }
  })
  if (tempStr.length) {
    return tempStr.join(',')
  } else {
    return str
  }
}

export const repayWayFormat = (str: string = '') => {
  if ((config.core.repayWayOptions as any)[str]) {
    return (config.core.repayWayOptions as any)[str]
  } else {
    return str
  }
}
export const pnltIntBaseFormat = (str: string = '') => {
  if ((config.core.pnltIntBaseOptions as any)[str]) {
    return (config.core.pnltIntBaseOptions as any)[str]
  } else {
    return str
  }
}
export const yearTypeFormat = (str: string = '') => {
  if ((config.core.yearTypeOptions as any)['_'.concat(str)]) {
    return (config.core.yearTypeOptions as any)['_'.concat(str)]
  } else {
    return str
  }
}
export const monthTypeFormat = (str: string = '') => {
  if ((config.core.monthTypeOptions as any)['_'.concat(str)]) {
    return (config.core.monthTypeOptions as any)['_'.concat(str)]
  } else if ((config.core.monthTypeOptions as any)[str]) {
    return (config.core.monthTypeOptions as any)[str]
  } else {
    return str
  }
}
export const prepayTypeFormat = (str: string = '') => {
  if ((config.core.prepayTypeOptions as any)[str]) {
    return (config.core.prepayTypeOptions as any)[str]
  } else {
    return str
  }
}
export const prepayTypeArrayFormat = (str: string = '') => {
  let tempStr: any = []
  let arrys = str.split(',')
  arrys.forEach(item => {
    if ((config.core.prepayTypeOptions as any)[item]) {
      tempStr.push((config.core.prepayTypeOptions as any)[item])
    }
  })
  if (tempStr.length) {
    return tempStr.join(',')
  } else {
    return str
  }
}

export const prepayIntTypeFormat = (str: string = '') => {
  if ((config.core.prepayIntTypeOptions as any)[str]) {
    return (config.core.prepayIntTypeOptions as any)[str]
  } else {
    return str
  }
}
export const prepayRepayPlanTypeFormat = (str: string = '') => {
  if ((config.core.prepayRepayPlanTypeOptions as any)[str]) {
    return (config.core.prepayRepayPlanTypeOptions as any)[str]
  } else {
    return str
  }
}
export const gracePnltIntTypeFormat = (str: string = '') => {
  if ((config.core.gracePnltIntTypeOptions as any)[str]) {
    return (config.core.gracePnltIntTypeOptions as any)[str]
  } else {
    return str
  }
}
export const feeTypeFormat = (str: string = '') => {
  if ((config.core.feeTypeOptions as any)[str]) {
    return (config.core.feeTypeOptions as any)[str]
  } else {
    return str
  }
}
export const yearRateTypeFormat = (str: string = '') => {
  if ((config.core.yearRateTypeOptions as any)[str]) {
    return (config.core.yearRateTypeOptions as any)[str]
  } else {
    return str
  }
}
export const ovdueStatusFormat = (str: string = '') => {
  if ((config.core.ovdueStatusOptions as any)[str]) {
    return (config.core.ovdueStatusOptions as any)[str]
  } else {
    return str
  }
}
export const accruedStatusFormat = (str: string = '') => {
  let str1 = str.replace('-', '')
  if ((config.core.accruedStatusOptions as any)[str1]) {
    return (config.core.accruedStatusOptions as any)[str1]
  } else {
    return str
  }
}
export const termStatusFormat = (str: string = '') => {
  if ((config.core.termStatusOptions as any)[str]) {
    return (config.core.termStatusOptions as any)[str]
  } else {
    return str
  }
}
export const repayTypeFormat = (str: string = '') => {
  if ((config.core.repayTypeOptions as any)[str]) {
    return (config.core.repayTypeOptions as any)[str]
  } else {
    return str
  }
}
export const coreStatusFormat = (str: string = '') => {
  if ((config.core.coreStatusOptions as any)[str]) {
    return (config.core.coreStatusOptions as any)[str]
  } else {
    return str
  }
}
export const orderStatusFormat = (str: string = '') => {
  if ((config.core.orderStatusOptions as any)[str]) {
    return (config.core.orderStatusOptions as any)[str]
  } else {
    return str
  }
}
export const repayByFormat = (str: string = '') => {
  if ((config.core.repayByOptions as any)[str]) {
    return (config.core.repayByOptions as any)[str]
  } else {
    return str
  }
}
export const certTypeFormat = (str: string = '') => {
  if ((config.core.certTypeOptions as any)[str]) {
    return (config.core.certTypeOptions as any)[str]
  } else {
    return str
  }
}
export const genderFormat = (str: string = '') => {
  if ((config.core.genderOptions as any)[str]) {
    return (config.core.genderOptions as any)[str]
  } else {
    return str
  }
}
export const marriageFormat = (str: string = '') => {
  if ((config.core.marriageOptions as any)[str]) {
    return (config.core.marriageOptions as any)[str]
  } else {
    return str
  }
}
export const educationFormat = (str: string = '') => {
  if ((config.core.educationOptions as any)[str]) {
    return (config.core.educationOptions as any)[str]
  } else {
    return str
  }
}
export const acctTypeFormat = (str: string = '') => {
  if ((config.core.acctTypeOptions as any)[str]) {
    return (config.core.acctTypeOptions as any)[str]
  } else {
    return str
  }
}
export const contractTypeFormat = (str: string = '') => {
  if ((config.core.contractTypeOptions as any)[str]) {
    return (config.core.contractTypeOptions as any)[str]
  } else {
    return str
  }
}
export const monthIncomeFormat = (str: string = '') => {
  if ((config.core.monthIncomeOptions as any)[str]) {
    return (config.core.monthIncomeOptions as any)[str]
  } else {
    return str
  }
}
export const dutyTypeFormat = (str: string = '') => {
  if ((config.core.dutyTypeOptions as any)[str]) {
    return (config.core.dutyTypeOptions as any)[str]
  } else {
    return str
  }
}
export const corpTypeFormat = (str: string = '') => {
  if ((config.core.corpTypeOptions as any)[str]) {
    return (config.core.corpTypeOptions as any)[str]
  } else {
    return str
  }
}
export const workStatusFormat = (str: string = '') => {
  if ((config.core.workStatusOptions as any)[str]) {
    return (config.core.workStatusOptions as any)[str]
  } else {
    return str
  }
}
export const industryFormat = (str: string = '') => {
  if ((config.core.industryOptions as any)[str]) {
    return (config.core.industryOptions as any)[str]
  } else {
    return str
  }
}
export const workYearFormat = (str: string = '') => {
  if ((config.core.workYearOptions as any)[str]) {
    return (config.core.workYearOptions as any)[str]
  } else {
    return str
  }
}
export const relationTypeFormat = (str: string = '') => {
  if ((config.core.relationTypeOptions as any)[str]) {
    return (config.core.relationTypeOptions as any)[str]
  } else {
    return str
  }
}
export const addressTypeFormat = (str: string = '') => {
  if ((config.core.addressTypeOptions as any)[str]) {
    return (config.core.addressTypeOptions as any)[str]
  } else {
    return str
  }
}
export const thirdPartyFormat = (str: string = '') => {
  if ((config.core.thirdPartyOptions as any)[str]) {
    return (config.core.thirdPartyOptions as any)[str]
  } else {
    return str
  }
}
// --------------------- 核心系统枚举处理：end ----------------------------

export const rateToPercent = (rate?: string | number) => {
  if (rate === undefined || Number(rate) === 0) {
    return '0.00%'
  } else if (typeof rate === 'string' && /^[0-9]+$/.test(rate)) {
    rate = Number(rate)
  }
  // 处理字符串转小数精度问题
  rate = Math.round(<number>rate * 100 * 1000) / 1000 + '%'
  return rate
}
export const rateToPercentVintage = (rate?: string | number) => {
  if (rate === undefined) {
    return ''
  } else if (Number(rate) === 0) {
    return '0.00%'
  } else if (typeof rate === 'string' && /^[0-9]+$/.test(rate)) {
    rate = Number(rate)
  }
  // 处理字符串转小数精度问题
  rate = Math.round(<number>rate * 100 * 1000) / 1000
  var xsd = rate.toString().split('.')
  if (xsd.length === 1) {
    rate = rate.toString() + '.00%'
    return rate
  }
  if (xsd.length > 1) {
    if (xsd[1].length < 2) {
      rate = rate.toString() + '0%'
      return rate
    }
  }
  return rate + '%'
}
export const marketFormat = (str: string = '') => {
  if ((config.core.marketTypeOptions as any)[str]) {
    return (config.core.marketTypeOptions as any)[str]
  } else {
    return str
  }
}
export const validFormat = (str: string = '') => {
  if ((config.core.isValidOptions as any)[str]) {
    return (config.core.isValidOptions as any)[str]
  } else {
    return str
  }
}
